package spring.cloud.tasks.executor.name_space.executor.task.task_listener.trigger_task_execute;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.node.BlockNodePaths;
import spring.cloud.tasks.common.node.ExecutorNodePath;
import spring.cloud.tasks.common.node.TaskNodePath;
import spring.cloud.tasks.executor.common.Service;
import spring.cloud.tasks.executor.name_space.executor.task.TaskExecutor;
import spring.cloud.tasks.executor.name_space.executor.task.task_listener.task_statistics.TaskStatistics;
import spring.cloud.tasks.executor.name_space.executor.task.task_listener.task_statistics.TaskStatisticsNode;

@Slf4j
@Setter
@Getter
public class TaskExecutorInfoService extends Service {


    public TaskExecutorInfoService(final TaskExecutor taskExecutor) {
        super(taskExecutor);
    }

    @Override
    public void start() {

    }

    /**
     * 持久化任务服务器上线相关信息.
     */
    public void persistExecutorOnline() {
        nameSpaceDataManager.createEphemeralDataNode(TaskNodePath.getTaskExecutorNode(taskId, executorId), "");
        nameSpaceDataManager.createEphemeralDataNode(BlockNodePaths.getExecutorTaskNode(executorId, taskId), "");
    }


    ////////////////////////////////////////////////////
    public void resetCount() {
        initTotalCountDelta();
        initFailureCountDelta();
        persistProcessSuccessCount(0);
        persistProcessFailureCount(0);

    }

    private void initTotalCountDelta() {
        // init total count.
        Integer count = 0;
        String nodePath = TaskNodePath.getTaskNodePath(taskId, TaskStatisticsNode.total_count_delta);
        if (!this.nameSpaceDataManager.checkExists(nodePath)) {
            this.nameSpaceDataManager.createIfNeedOrSetData(nodePath, count.toString());
        }
        TaskStatistics.initTotalCountDelta(taskId, executorId, 0);
    }

    private void initFailureCountDelta() {
        // init error count.
        Integer count = 0;
        String nodePath = TaskNodePath.getTaskNodePath(taskId, TaskStatisticsNode.failure_count_delta);
        if (!this.nameSpaceDataManager.checkExists(nodePath)) {
            this.nameSpaceDataManager.createIfNeedOrSetData(nodePath, count.toString());
        }
        TaskStatistics.initFailureCountDelta(taskId, executorId, 0);
    }

    public synchronized void persistTotalCountDelta() {
        int delta = TaskStatistics.getTotalCountDelta(taskId, executorId);
        if (delta > 0) {
            String nodePath = TaskNodePath.getTaskNodePath(taskId, TaskStatisticsNode.total_count_delta);
            int $delta = Integer.parseInt(this.nameSpaceDataManager.getData(nodePath));
            this.nameSpaceDataManager.createIfNeedOrSetData(nodePath, String.valueOf($delta + delta));
            TaskStatistics.initTotalCountDelta(taskId, executorId, 0);
        }
    }

    public synchronized void persistFailureCountDelta() {
        int delta = TaskStatistics.getFailureCountDelta(taskId, executorId);
        if (delta > 0) {
            String nodePath = TaskNodePath.getTaskNodePath(taskId, TaskStatisticsNode.failure_count_delta);
            int $delta = Integer.parseInt(this.nameSpaceDataManager.getData(nodePath));
            this.nameSpaceDataManager.createIfNeedOrSetData(nodePath, String.valueOf($delta + delta));
            TaskStatistics.initFailureCountDelta(taskId, executorId, 0);
        }
    }

    /**
     * 持久化统计处理数据成功的数量的数据.
     *
     * @param processSuccessCount 成功数
     */
    public void persistProcessSuccessCount(final int processSuccessCount) {
        String nodePath = ExecutorNodePath.getProcessSuccessCountNode(executorId);
        this.nameSpaceDataManager.createIfNeedOrSetData(nodePath, String.valueOf(processSuccessCount));
    }

    /**
     * 持久化统计处理数据失败的数量的数据.
     *
     * @param processFailureCount 失败数
     */
    public void persistProcessFailureCount(final int processFailureCount) {
        String nodePath = ExecutorNodePath.getProcessFailureCountNode(executorId);
        this.nameSpaceDataManager.createIfNeedOrSetData(nodePath, String.valueOf(processFailureCount));
    }


    /**
     * 清除立即运行的标记
     */
    public void clearRunOneTimePath() {
        String nodePath = ExecutorNodePath.getRunOneTimePath(executorId);
        try {
            this.nameSpaceDataManager.delete(nodePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除立即终止任务的标记
     */
    public void clearStopOneTimePath() {
        String nodePath = ExecutorNodePath.getStopOneTimePath(executorId);
        try {
            this.nameSpaceDataManager.delete(nodePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
